<?php
namespace app\index\model;
use think\Model;
use think\Db;
use think\Session;
use think\Controller;

class Inbox_model
{
	// 发送站内信
	public function getindex()
	{
		
		$uid = $_SESSION['think']['username'][0]['user_id'];
        $time =  date('Y-m-d H:i:s',time());
        $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];
        if ($prevdata !== '我的消息') {
            //记录用户操作
            $data = [
                'user_id' => $uid,  
                'log_action' => 'Inbox/index',   
                'log_desc' => '我的消息',   //默认空
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $res = db('_user_log')
                ->insert($data);
        }
        //记录用户操作
        
        $list = Db::table('cn_user')
                ->alias('cu')
                ->join('cn_user_character_relation cucr','cu.user_id = cucr.user_id')
                ->join('cn_character cc','cc.chara_id = cucr.chara_id')
                ->join('cn_menu_character_relation cmcr','cmcr.chara_id = cc.chara_id')
                ->join('cn_menu cm','cmcr.menu_id = cm.menu_id')
                ->where('cn_user.user_id',$uid)
                ->field('menu_name')
                ->find();
       //  //查询有多少消息 未读已读
        // 查询该id所对应的角色，组织，人员
        $oid = Db::table('cn_user_organization_relation') 
                ->alias('cuor')
                ->join('cn_organization co','cuor.org_id = co.org_id')
                ->where('cuor.user_id',$uid)
                ->field('cuor.org_id')
                ->find();
        // $oid = $oid['org_id'];
        // var_dump($oid);die;
        $cid = Db::table('cn_user_character_relation') 
                ->alias('cucr')
                ->join('cn_character cc','cucr.chara_id = cc.chara_id')
                ->where('cucr.user_id',$uid)
                ->field('cucr.chara_id')
                ->find();

        $omail = Db::table('cn_mail_addressee')
                ->alias('cma')
                ->join('cn_mail cm','cm.mail_id = cma.mail_id')
                ->where('cm.is_delete','eq','0')
                ->where('cm.mail_send_channel','eq','1')
                ->where('cma.add_org_id',$oid['org_id'])
                ->field('cm.mail_title,cm.mail_show_content,cm.create_time,cm.mail_id,cm.is_read')
                ->order('create_time desc')
                ->select();
        $mmail = Db::table('cn_mail_addressee')
                ->alias('cma')
                ->join('cn_mail cm','cm.mail_id = cma.mail_id')
                ->where('cm.is_delete','eq','0')
                ->where('cm.mail_send_channel','eq','1')
                ->where('cma.add_org_id','-1')
                ->field('cm.mail_title,cm.mail_show_content,cm.create_time,cm.mail_id,cm.is_read')
                ->order('create_time desc')
                ->select();
                // var_dump($omail);die;

        $cmail = Db::table('cn_mail_addressee')
                ->alias('cma')
                ->join('cn_mail cm','cm.mail_id = cma.mail_id')
                ->where('cm.is_delete','eq','0')
                ->where('cm.mail_send_channel','eq','1')
                ->where('cma.add_chara_id',$cid['chara_id'])
                ->field('cm.mail_title,cm.mail_show_content,cm.create_time,cm.mail_id,cm.is_read')
                ->order('create_time desc')
                ->select();
                // var_dump($cmail);die;
        $umail = Db::table('cn_mail_addressee')
                ->alias('cma')
                ->join('cn_mail cm','cm.mail_id = cma.mail_id')
                ->where('cm.is_delete','eq','0')
                ->where('cm.mail_send_channel','eq','1')
                ->where('cma.add_user_id',$uid)
                ->field('cm.mail_title,cm.mail_show_content,cm.create_time,cm.mail_id,cm.is_read')
                ->order('create_time desc')
                ->select();
                // var_dump($umail);die;

        $mail = array_merge_recursive($omail,$cmail,$umail,$mmail);
        // var_dump($mail);die;
        //         ->paginate(10);
        // $page = $mail->render();
               

        if (empty($mail)) {
            $amount = 0;
        }else{
       
     }
        $amount = $mail;
        // 查询该id所对应的角色，组织，人员
    
       
        // 查询有多少消息未读的个数
        $onum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_org_id',$oid['org_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $cnum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_chara_id',$cid['chara_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $unum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_user_id',$uid)
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $allnum = array_merge_recursive($onum,$cnum,$unum);
        $num = count($allnum);
        // var_dump($num);die;
        $ores = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_org_id',$oid['org_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $mres = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_org_id','-1')
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        // dump($mres);die;
        $cres = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_chara_id', $cid['chara_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ures = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_user_id',$uid)
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $sql = "SELECT
                    cm.mail_title,
                    cm.mail_id,
                    count( cm.mail_id ) AS count,
                    cm.mail_content,
                    cm.create_time,
                    cm.mail_id,
                    cm.is_read 
                FROM
                    cn_mail AS cm
                    LEFT JOIN cn_mail_addressee AS cma ON cma.mail_id = cm.mail_id 
                WHERE
                    cma.add_org_id = 1 
                    OR cma.add_org_id = - 1 
                    AND cm.is_delete = 0 
                    AND cm.is_read = 0 
                    AND cm.mail_send_channel = 2 
                GROUP BY
                    cm.mail_id";
        $ores = Db::query($sql);
       
        $ares = array_merge_recursive($ores,$cres,$ures,$mres);
        // dump($ares);die;
        $num2 = (count($ares));
        
       
        $info = [
                'list' => $list,
                'amount' => $amount,
                'num' => $num,
                'num2' => $num2,
                // 'page' => $page
            ];
        // var_dump($info);die;
        return $info;
      
	}

	public function getmail()
	{
		$uid = $_SESSION['think']['username'][0]['user_id'];
        $time =  date('Y-m-d H:i:s',time());
        $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];
        if ($prevdata !== '写邮件') {
            //记录用户操作
            $data = [
                'user_id' => $uid,  
                'log_action' => 'Inbox/index',   
                'log_desc' => '写邮件',   
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $res = db('_user_log')
                ->insert($data);
        }
        
        $list = Db::table('cn_user')
                ->alias('cu')
                ->join('cn_user_character_relation cucr','cu.user_id = cucr.user_id')
                ->join('cn_character cc','cc.chara_id = cucr.chara_id')
                ->join('cn_menu_character_relation cmcr','cmcr.chara_id = cc.chara_id')
                ->join('cn_menu cm','cmcr.menu_id = cm.menu_id')
                ->where('cn_user.user_id',$uid)
                ->field('menu_name')
                ->select();
        //获取所有人员、角色、组织（去除自己）
        $alluser = Db::table('cn_user')
                ->where('user_id','neq',$uid)
                ->field('user_name')
                ->select();
        // var_dump($alluser);die;
        for ($i=0; $i < count($alluser) ; $i++) { 
            $au[$i]['name'] = $alluser[$i]['user_name'];
        }
                // var_dump($au);die;
        // foreach($alluser as $k=>$v){
        //     $b[$k]['user_name'] = $v['user_name'];
        //     $b[$k]['user_id'] = $v['user_id'];
        // }
        // var_dump($b);die;
        $allchar = Db::table('cn_character')
                ->field('chara_name')
                ->select();
        for ($i=0; $i < count($allchar) ; $i++) { 
            $ac[$i]['name'] = $allchar[$i]['chara_name'];
        }
        // var_dump($allchar);die;
        // foreach($allchar as $l=>$f){
        //     $n[$l] = $f['chara_name'];
        //     $n[$l] = $f['chara_id'];
        // }
        // var_dump($n);die;
        $allorg = Db::table('cn_organization')
                ->field('org_name')
                ->select();
        for ($i=0; $i < count($allorg) ; $i++) { 
            $ao[$i]['name'] = $allorg[$i]['org_name'];
        }
                // var_dump($allorg);die;
        // foreach($allorg as $j=>$r){
        //     $m[$j] = $r['org_name'];
        //     $m[$j] = $r['org_name'];
        // }
        // 信息合并到一个二维数组
        $all = array_merge_recursive($au,$ac,$ao);
        // $all = $b;
        // var_dump($all);die;
        // 查询该id所对应的角色，组织，人员
        $oid = Db::table('cn_user_organization_relation') 
                ->alias('cuor')
                ->join('cn_organization co','cuor.org_id = co.org_id')
                ->where('cuor.user_id',$uid)
                ->field('cuor.org_id')
                ->find();
                // var_dump($oid);die;
        $cid = Db::table('cn_user_character_relation') 
                ->alias('cucr')
                ->join('cn_character cc','cucr.chara_id = cc.chara_id')
                ->where('cucr.user_id',$uid)
                ->field('cucr.chara_id')
                ->find();
       
        $onum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_org_id',$oid['org_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $cnum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_chara_id',$cid['chara_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $unum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_user_id',$uid)
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $allnum = array_merge_recursive($onum,$cnum,$unum);
        $num = count($allnum);
        // var_dump($num);die;
        $ores = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_org_id',$oid['org_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $cres = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_chara_id', $cid['chara_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ures = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_user_id',$uid)
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ares = array_merge_recursive($ores,$cres,$ures);
        $num2 = (count($ares));
        if (empty($num)) {
            $num = 0;
            if (empty($num2)) {
                $num2 = 0;
                $info = [
                'all' => $all,
                'node' => $list,
                'num' => $num,
                'num2' => $num2
            ];
            }
             $info = [
                'all' => $all,
                'node' => $list,
                'num' => $num,
                'num2' => $num2
            ];
        }else{
            $info = [
                'all' => $all,
                'node' => $list,
                'num' => $num,
                'num2' => $num2
            ];
        }
        return $info;
	}

	public function sendMail()
	{
        $uid = $_SESSION['think']['username'][0]['user_id'];
        $time =  date('Y-m-d H:i:s',time());
        $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];
        if ($prevdata !== '发送邮件') {
            //记录用户操作
            $data = [
                'user_id' => $uid,  
                'log_action' => 'Inbox/sendMail',   
                'log_desc' => '发送邮件',  
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $res = db('_user_log')
                ->insert($data);
        }
       
        $list = Db::table('cn_user')
                ->alias('cu')
                ->join('cn_user_character_relation cucr','cu.user_id = cucr.user_id')
                ->join('cn_character cc','cc.chara_id = cucr.chara_id')
                ->join('cn_menu_character_relation cmcr','cmcr.chara_id = cc.chara_id')
                ->join('cn_menu cm','cmcr.menu_id = cm.menu_id')
                ->where('cn_user.user_id',$uid)
                ->field('menu_name')
                ->select();
		//写入cn_mail
        if (empty($_POST['editorValue'])) {
            $_POST['editorValue'] = '';
        }
		$idata = [
			'mail_title' => $_POST['subject'],
			'mail_content' => $_POST['editorValue'],
            'mail_show_content' => strip_tags($_POST['editorValue']),
			'mail_type' => $_POST['type'],
			'mail_file' => '0',   //先写死
			'mail_send_type' => '2',
			'mail_type' => $_POST['type'],
			'mail_secrecy' => '0', //默认不保密
			'mail_send_channel' => '1',  //发送给我的
			'create_user_id' => $uid,
			'create_time' => $time,
			'update_user_id' => $uid,
			'update_time' => $time,
            'is_read' => '0',
			'is_delete' => '0'
		];
		$res = db('_mail')
            ->insertGetId($idata);
        // var_dump($_POST);die;
        $to[] = $_POST['to'];
        $t = $to[0];
        // var_dump($t);die;
        for ($i=0; $i < count($t); $i++) { 
               // 根据收件人名获取组织id
            $req[$i] = Db::table('cn_organization')
                    ->alias('co')
                    ->where('co.org_name',$t[$i])
                    ->field('org_id')
                    ->find();
            $org_id[$i] = $req[$i]['org_id'];
            }
            // var_dump($org_id);die;
            if ($org_id[0]) {
                // var_dump(222);die;
                for ($j=0; $j < count($org_id) ; $j++) { 
                    # code...
                $info[$j] = [
                    'add_type' => '1',
                    'add_org_id' => $org_id[$j],
                    'add_chara_id' => '0',
                    'add_user_id' => '0',
                    'mail_id' => $res,
                    'create_user_id' => $uid,
                    'create_time' => $time,
                    'update_user_id' => $uid,
                    'update_time' => $time,
                    'is_delete' => '0',
                ];
                    $a = db('_mail_addressee')
                        ->insert($info[$j]);
                }
                // var_dump($info);die;
                        
            }else{
                for ($i=0; $i < count($t); $i++) { 

                       // 根据收件人名获取组织id
                
                        $rew = Db::table('cn_user')
                        ->alias('cu')
                        ->where('cu.user_name',$t[$i])
                        ->field('user_id')
                        ->find();
                    $user_id[$i] = $rew['user_id'];
                }
                    // var_dump($user_id);die;
                if ($user_id[0]) {
                    for ($m=0; $m < count($user_id) ; $m++) { 
                        # code...
                    
                    $info[$m] = [
                        'add_type' => '3',
                        'add_org_id' => '0',
                        'add_chara_id' => '0',
                        'add_user_id' => $user_id[$m],
                        'mail_id' => $res,
                        'create_user_id' => $uid,
                        'create_time' => $time,
                        'update_user_id' => $uid,
                        'update_time' => $time,
                        'is_delete' => '0',
                    ];
                    $a = db('_mail_addressee')
                        ->insert($info[$m]);
                    }   
                }else{
                    for ($i=0; $i < count($t); $i++) { 
                       $ree = Db::table('cn_character')
                        ->alias('cc')
                        ->where('cc.chara_name',$t[$i])
                        ->field('chara_id')
                        ->find();
                    $chara_id[$i] = $ree['chara_id'];
                    }
                    // var_dump($chara_id);die;
                    for ($v=0; $v < count($chara_id); $v++) { 
                        # code...
                    
                    $info[$v] = [
                        'add_type' => '2',
                        'add_org_id' => '0' ,
                        'add_chara_id' => $chara_id[$v],
                        'add_user_id' => '0',
                        'mail_id' => $res,
                        'create_user_id' => $uid,
                        'create_time' => $time,
                        'update_user_id' => $uid,
                        'update_time' => $time,
                        'is_delete' => '0',
                    ];
                    $a = db('_mail_addressee')
                        ->insert($info[$v]);
                    }    
                }
                        $b = [
                            'a' =>$a,
                            'node' => $list
                            ];
                        return $b;


                
            }
	}


    public function getMailinfo($id)
    {
        $uid = $_SESSION['think']['username'][0]['user_id'];
        $time =  date('Y-m-d H:i:s',time());
        $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];
        if ($prevdata !== '查看消息') {
            //记录用户操作
            $data = [
                'user_id' => $uid,  
                'log_action' => 'Inbox/getMailinfo',   
                'log_desc' => '查看消息',  
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $ires = db('_user_log')
                ->insert($data);
        }
       
        $list = Db::table('cn_user')
                ->alias('cu')
                ->join('cn_user_character_relation cucr','cu.user_id = cucr.user_id')
                ->join('cn_character cc','cc.chara_id = cucr.chara_id')
                ->join('cn_menu_character_relation cmcr','cmcr.chara_id = cc.chara_id')
                ->join('cn_menu cm','cmcr.menu_id = cm.menu_id')
                ->where('cn_user.user_id',$uid)
                ->field('menu_name')
                ->select();
        
        // 根据mail_id 查询  消息的标签  内容
        $mailinfo = Db::table('cn_mail_addressee')
                    ->alias('cma')
                    ->join('cn_mail cm','cm.mail_id = cma.mail_id')
                    ->where('cma.mail_id',$id)
                    ->where('cma.is_delete','eq','0')
                    ->where('cm.is_delete','eq','0')
                    ->field('cm.mail_content,cm.mail_title,mail_type,cma.create_user_id,cma.create_time,cma.mail_id,add_org_id,add_chara_id,add_user_id')
                    ->find();
        //根据create_id查询发件人是谁
        if ($mailinfo['add_org_id']) {
            //查询org_id对应org
            $fj = Db::table('cn_organization')
                    ->where('org_id',$mailinfo['add_org_id'])
                    ->field('org_name')
                    ->find();
            $fj = $fj['org_name'];
        }else if($mailinfo['add_chara_id']){
            //查询chara_id对应chara
            $fj = Db::table('cn_character')
                    ->where('chara_id',$mailinfo['add_chara_id'])
                    ->field('chara_name')
                    ->find();
            $fj = $fj['chara_name'];
        }else{
            //查询user_id对应user
            $fj = Db::table('cn_user')
                    ->where('user_id',$mailinfo['add_user_id'])
                    ->field('user_name')
                    ->find();
            $fj = $fj['user_name'];
        }
        // 更改消息为已读
        $read = Db::table('cn_mail')
                ->where('cn_mail.mail_id',$id)
                ->update(['is_read'=> '1']);
        
        // 查询该id所对应的角色，组织，人员
        $oid = Db::table('cn_user_organization_relation') 
                ->alias('cuor')
                ->join('cn_organization co','cuor.org_id = co.org_id')
                ->where('cuor.user_id',$uid)
                ->field('cuor.org_id')
                ->find();
        $cid = Db::table('cn_user_character_relation') 
                ->alias('cucr')
                ->join('cn_character cc','cucr.chara_id = cc.chara_id')
                ->where('cucr.user_id',$uid)
                ->field('cucr.chara_id')
                ->find();
       
        $onum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_org_id',$oid['org_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $cnum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_chara_id',$cid['chara_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $unum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_user_id',$uid)
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $allnum = array_merge_recursive($onum,$cnum,$unum);
        $num = count($allnum);
        // var_dump($num);die;
        $ores = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_org_id',$oid['org_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $cres = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_chara_id', $cid['chara_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ures = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_user_id',$uid)
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ares = array_merge_recursive($ores,$cres,$ures);
        $num2 = (count($ares));
        if (empty($num)) {
            $num = 0;
            if (empty($num2)) {
                 $num2 = 0;
                 $info = [
                'node' => $list,
                'mailinfo' => $mailinfo,
                'fj' => $fj,
                'num' => $num,
                'num2' => $num2
            ];
            }
             $info = [
                'node' => $list,
                'mailinfo' => $mailinfo,
                'fj' => $fj,
                'num' => $num,
                'num2' => $num2
            ];
        }else{
            $info = [
                'node' => $list,
                'mailinfo' => $mailinfo,
                'fj' => $fj,
                'num' => $num,
                'num2' => $num2
            ];
        }
        return $info;
    }

    public function getnMailinfo($id)
    {
        $uid = $_SESSION['think']['username'][0]['user_id'];
        $time =  date('Y-m-d H:i:s',time());
        $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];
        if ($prevdata !== '查看公告消息') {
            //记录用户操作
            $data = [
                'user_id' => $uid,  
                'log_action' => 'Inbox/getnMailinfo',   
                'log_desc' => '查看公告消息', 
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $ires = db('_user_log')
                ->insert($data);
        }
       
        $list = Db::table('cn_user')
                ->alias('cu')
                ->join('cn_user_character_relation cucr','cu.user_id = cucr.user_id')
                ->join('cn_character cc','cc.chara_id = cucr.chara_id')
                ->join('cn_menu_character_relation cmcr','cmcr.chara_id = cc.chara_id')
                ->join('cn_menu cm','cmcr.menu_id = cm.menu_id')
                ->where('cn_user.user_id',$uid)
                ->field('menu_name')
                ->select();
        
        // 根据mail_id 查询  消息的标签  内容
        $mailinfo = Db::table('cn_mail_addressee')
                    ->alias('cma')
                    ->join('cn_mail cm','cm.mail_id = cma.mail_id')
                    ->where('cma.mail_id',$id)
                    ->where('cma.is_delete','eq','0')
                    ->where('cm.is_delete','eq','0')
                    ->field('cm.mail_content,cm.mail_title,mail_type,cma.create_user_id,cma.create_time,cma.mail_id')
                    ->find();
        //根据create_id查询发件人是谁
        $fjid = Db::table('cn_user')
                    ->alias('cu')
                    ->where('cu.user_id',$mailinfo['create_user_id'])
                    ->field('cu.user_name')
                    ->select();
        // 更改消息为已读
        $read = Db::table('cn_mail')
                ->where('cn_mail.mail_id',$id)
                ->update(['is_read'=> '1']);
        
        // 查询该id所对应的角色，组织，人员
        $oid = Db::table('cn_user_organization_relation') 
                ->alias('cuor')
                ->join('cn_organization co','cuor.org_id = co.org_id')
                ->where('cuor.user_id',$uid)
                ->field('cuor.org_id')
                ->find();
        $cid = Db::table('cn_user_character_relation') 
                ->alias('cucr')
                ->join('cn_character cc','cucr.chara_id = cc.chara_id')
                ->where('cucr.user_id',$uid)
                ->field('cucr.chara_id')
                ->find();
       
        $onum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_org_id',$oid['org_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $cnum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_chara_id',$cid['chara_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $unum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_user_id',$uid)
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $allnum = array_merge_recursive($onum,$cnum,$unum);
        $num = count($allnum);
        // var_dump($num);die;
        $ores = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_org_id',$oid['org_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $cres = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_chara_id', $cid['chara_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ures = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_user_id',$uid)
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ares = array_merge_recursive($ores,$cres,$ures);
        $num2 = (count($ares));
        if (empty($num)) {
            $num = 0;
            if (empty($num2)) {
                 $num2 = 0;
                 $info = [
                'node' => $list,
                'mailinfo' => $mailinfo,
                'fjid' => $fjid,
                'num' => $num,
                'num2' => $num2
            ];
            }
             $info = [
                'node' => $list,
                'mailinfo' => $mailinfo,
                'fjid' => $fjid,
                'num' => $num,
                'num2' => $num2
            ];
        }else{
            $info = [
                'node' => $list,
                'mailinfo' => $mailinfo,
                'fjid' => $fjid,
                'num' => $num,
                'num2' => $num2
            ];
        }
       
        return $info;
    }


    // 已发送
    public function getSentmail()
    {
        $uid = $_SESSION['think']['username'][0]['user_id'];
        $time =  date('Y-m-d H:i:s',time());
        $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];
        if ($prevdata !== '查看已发送') {
            //记录用户操作
            $data = [
                'user_id' => $uid,  
                'log_action' => 'Inbox/getSentmail',   
                'log_desc' => '查看已发送', 
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $ires = db('_user_log')
                ->insert($data);
        }
       
        $list = Db::table('cn_user')
                ->alias('cu')
                ->join('cn_user_character_relation cucr','cu.user_id = cucr.user_id')
                ->join('cn_character cc','cc.chara_id = cucr.chara_id')
                ->join('cn_menu_character_relation cmcr','cmcr.chara_id = cc.chara_id')
                ->join('cn_menu cm','cmcr.menu_id = cm.menu_id')
                ->where('cn_user.user_id',$uid)
                ->field('menu_name')
                ->select();
        // 查询该id所对应的角色，组织，人员
        $oid = Db::table('cn_user_organization_relation') 
                ->alias('cuor')
                ->join('cn_organization co','cuor.org_id = co.org_id')
                ->where('cuor.user_id',$uid)
                ->field('cuor.org_id')
                ->find();
        $cid = Db::table('cn_user_character_relation') 
                ->alias('cucr')
                ->join('cn_character cc','cucr.chara_id = cc.chara_id')
                ->where('cucr.user_id',$uid)
                ->field('cucr.chara_id')
                ->find();
        $total = Db::table('cn_mail_addressee')
                ->alias('cma')
                ->join('cn_mail cm','cm.mail_id = cma.mail_id')
                ->where('cm.create_user_id',$uid)
                ->where('cm.is_delete','eq','0')
                ->field('cm.mail_title,cm.mail_show_content,cm.create_time,cm.mail_id,cm.is_read')
                ->order('create_time desc')
                ->paginate(10);
        $page = $total->render();
        $amount = $total;

        if (empty($total)) {
            $amount = 0;
        }
        // 查询有多少消息未读的个数
        $onum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_org_id',$oid['org_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $cnum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_chara_id',$cid['chara_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $unum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_user_id',$uid)
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $allnum = array_merge_recursive($onum,$cnum,$unum);
        $num = count($allnum);
        // var_dump($num);die;
        $ores = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_org_id',$oid['org_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $cres = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_chara_id', $cid['chara_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ures = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_user_id',$uid)
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ares = array_merge_recursive($ores,$cres,$ures);
        $num2 = (count($ares));
        if (empty($num)) {
            $num = 0;
            if (empty($num2)) {
                $num2 = 0;
                $info = [
                'list' => $list,
                'amount' => $amount,
                'num' => $num,
                'num2' => $num2,
                'page' => $page
            ];
            }
             $info = [
                'list' => $list,
                'amount' => $amount,
                'num' => $num,
                'num2' => $num2,
                'page' => $page

            ];
        }else{
            $info = [
                'list' => $list,
                'amount' => $amount,
                'num' => $num,
                'num2' => $num2,
                'page' => $page

            ];
        }
       
        return $info;
    }

    // 已发消息
    public function getMailalready($id)
    {
        $uid = $_SESSION['think']['username'][0]['user_id'];
        $time =  date('Y-m-d H:i:s',time());
        $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];
        if ($prevdata !== '查看已发消息') {
            //记录用户操作
            $data = [
                'user_id' => $uid,  
                'log_action' => 'Inbox/getMailalready',   
                'log_desc' => '查看已发消息', 
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $ires = db('_user_log')
                ->insert($data);
        }
        $list = Db::table('cn_user')
                ->alias('cu')
                ->join('cn_user_character_relation cucr','cu.user_id = cucr.user_id')
                ->join('cn_character cc','cc.chara_id = cucr.chara_id')
                ->join('cn_menu_character_relation cmcr','cmcr.chara_id = cc.chara_id')
                ->join('cn_menu cm','cmcr.menu_id = cm.menu_id')
                ->where('cn_user.user_id',$uid)
                ->field('menu_name')
                ->select();
        
        // 根据消息id查询消息内容
        $mailinfo = Db::table('cn_mail_addressee')
                    ->alias('cma')
                    ->join('cn_mail cm','cm.mail_id = cma.mail_id')
                    ->where('cma.mail_id',$id)
                    ->where('cma.is_delete','eq','0')
                    ->where('cm.is_delete','eq','0')
                    ->field('cm.mail_content,cm.mail_title,mail_type,cma.create_time,cma.add_type,cma.add_org_id,cma.add_user_id,cma.add_chara_id')
                    ->find();
        // 收件人信息
        if ($mailinfo['add_type'] == 1) {
            $org_id = $mailinfo['add_org_id'];
             $sjid = Db::table('cn_organization')
                    ->alias('co')
                    ->where('co.org_id',$org_id)
                    ->field('co.org_name')
                    ->find();
            $sj = $sjid['org_name'];
        }elseif ($mailinfo['add_type'] == 2) {
            $chara_id = $mailinfo['add_chara_id'];
             $sjid = Db::table('cn_character')
                    ->alias('cc')
                    ->where('cc.chara_id',$chara_id)
                    ->field('cc.chara_name')
                    ->find();
            $sj = $sjid['chara_name'];
        }elseif ($mailinfo['add_type'] == 3) {
            $user_id = $mailinfo['add_user_id'];
            $sjid = Db::table('cn_user')
                    ->alias('cu')
                    ->where('cu.user_id',$user_id)
                    ->field('cu.user_name')
                    ->find();
            $sj = $sjid['user_name'];
        }

        // 查询该id所对应的角色，组织，人员
        $oid = Db::table('cn_user_organization_relation') 
                ->alias('cuor')
                ->join('cn_organization co','cuor.org_id = co.org_id')
                ->where('cuor.user_id',$uid)
                ->field('cuor.org_id')
                ->find();
        $cid = Db::table('cn_user_character_relation') 
                ->alias('cucr')
                ->join('cn_character cc','cucr.chara_id = cc.chara_id')
                ->where('cucr.user_id',$uid)
                ->field('cucr.chara_id')
                ->find();
       
        $onum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_org_id',$oid['org_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $cnum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_chara_id',$cid['chara_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $unum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_user_id',$uid)
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $allnum = array_merge_recursive($onum,$cnum,$unum);
        $num = count($allnum);
        // var_dump($num);die;
        $ores = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_org_id',$oid['org_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $cres = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_chara_id', $cid['chara_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ures = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_user_id',$uid)
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ares = array_merge_recursive($ores,$cres,$ures);
        $num2 = (count($ares));
        if (empty($num)) {
            $num = 0;
            if (empty($num2)) {
                $num2 = 0;
                $info = [
                'node' => $list,
                'mailinfo' => $mailinfo,
                'sj' => $sj,
                'num' => $num,
                'num2' => $num2
            ];
            }
             $info = [
                'node' => $list,
                'mailinfo' => $mailinfo,
                'sj' => $sj,
                'num' => $num,
                'num2' => $num2['0']
            ];
        }else{
            $info = [
                'node' => $list,
                'mailinfo' => $mailinfo,
                'sj' => $sj,
                'num' => $num['0'],
                'num2' => $num2
            ];
        }
       
        return $info;
    }

    //删除消息
    public function delMail($id)
    {
        $uid = $_SESSION['think']['username'][0]['user_id'];
        $time =  date('Y-m-d H:i:s',time());
        $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];
        if ($prevdata !== '删除消息') {
            //记录用户操作
            $data = [
                'user_id' => $uid,  
                'log_action' => 'Inbox/delMail',   
                'log_desc' => '删除消息',  
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $ires = db('_user_log')
                ->insert($data);
        }
        for ($i=0; $i < count($id); $i++) { 
            $code[] = $id[$i];
        $delm = Db::table('cn_mail')
                ->alias('cm')
                ->where('cm.mail_id',$code[$i])
                ->update(['is_delete'=>1,'update_time'=>$time,'update_user_id'=>$uid]);
        $delam = Db::table('cn_mail_addressee')
                ->alias('cma')
                ->where('cma.mail_id',$code[$i])
                ->update(['is_delete'=>1,'update_time'=>$time,'update_user_id'=>$uid]);
        }
         return $delm;
    }

    public function getTrash()
    {
        $uid = $_SESSION['think']['username'][0]['user_id'];
        $time =  date('Y-m-d H:i:s',time());
        $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];
        if ($prevdata !== '浏览垃圾箱') {
            //记录用户操作
            $data = [
                'user_id' => $uid,  
                'log_action' => 'Inbox/getTrash',   
                'log_desc' => '浏览垃圾箱',   //默认空
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $ires = db('_user_log')
                ->insert($data);
        }
       
        $list = Db::table('cn_user')
                ->alias('cu')
                ->join('cn_user_character_relation cucr','cu.user_id = cucr.user_id')
                ->join('cn_character cc','cc.chara_id = cucr.chara_id')
                ->join('cn_menu_character_relation cmcr','cmcr.chara_id = cc.chara_id')
                ->join('cn_menu cm','cmcr.menu_id = cm.menu_id')
                ->where('cn_user.user_id',$uid)
                ->field('menu_name')
                ->select();
        // 查询有多少被删除
        $del = Db::table('cn_mail')
                ->alias('cm')
                ->where('is_delete','eq','1')
                ->field('mail_id,mail_title,mail_show_content,create_time')
                ->order('update_time desc')
                ->paginate(10);
        $page = $del->render();
        $amount = $del;
        if (empty($del)) {
            $amount = 0;
        }
        
        // 公告
        // 查询该id所对应的角色，组织，人员
        $oid = Db::table('cn_user_organization_relation') 
                ->alias('cuor')
                ->join('cn_organization co','cuor.org_id = co.org_id')
                ->where('cuor.user_id',$uid)
                ->field('cuor.org_id')
                ->find();
        $cid = Db::table('cn_user_character_relation') 
                ->alias('cucr')
                ->join('cn_character cc','cucr.chara_id = cc.chara_id')
                ->where('cucr.user_id',$uid)
                ->field('cucr.chara_id')
                ->find();
        // 查询有多少消息未读的个数
        $onum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_org_id',$oid['org_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $cnum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_chara_id',$cid['chara_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $unum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_user_id',$uid)
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $allnum = array_merge_recursive($onum,$cnum,$unum);
        $num = count($allnum);
        // var_dump($num);die;
        $ores = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_org_id',$oid['org_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $cres = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_chara_id', $cid['chara_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ures = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_user_id',$uid)
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ares = array_merge_recursive($ores,$cres,$ures);
        $num2 = (count($ares));
        if (empty($num)) {
            $num = 0;
            if (empty($num2)) {
                $num2 = 0;
                $info = [
                'list' => $list,
                'amount' => $amount,
                'num' => $num,
                'num2' => $num2,
                'page' => $page
            ];
            }
             $info = [
                'list' => $list,
                'amount' => $amount,
                'num' => $num,
                'num2' => $num2,
                'page' => $page
            ];
        }else{
            $info = [
                'list' => $list,
                'amount' => $amount,
                'num' => $num,
                'num2' => $num2,
                'page' => $page
            ];
        }
        return $info;
    }

    //已删消息内容
    public function getMaildel($id)
    {
        $uid = $_SESSION['think']['username'][0]['user_id'];
        $time =  date('Y-m-d H:i:s',time());
        $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];
        if ($prevdata !== '查看已删消息') {
            //记录用户操作
            $data = [
                'user_id' => $uid,  
                'log_action' => 'Inbox/getMaildel',   
                'log_desc' => '查看已删消息',   //默认空
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $ires = db('_user_log')
                ->insert($data);
        }
       
        $list = Db::table('cn_user')
                ->alias('cu')
                ->join('cn_user_character_relation cucr','cu.user_id = cucr.user_id')
                ->join('cn_character cc','cc.chara_id = cucr.chara_id')
                ->join('cn_menu_character_relation cmcr','cmcr.chara_id = cc.chara_id')
                ->join('cn_menu cm','cmcr.menu_id = cm.menu_id')
                ->where('cn_user.user_id',$uid)
                ->field('menu_name')
                ->select();
        
        // 根据消息id查询消息内容
        $mailinfo = Db::table('cn_mail_addressee')
                    ->alias('cma')
                    ->join('cn_mail cm','cm.mail_id = cma.mail_id')
                    ->where('cma.mail_id',$id)
                    ->where('cma.is_delete','eq','1')
                    ->where('cm.is_delete','eq','1')
                    ->field('cm.mail_content,cm.mail_title,mail_type,cma.create_time,cma.add_type,cma.add_org_id,cma.add_user_id,cma.add_chara_id')
                    ->find();
        // 收件人信息
        if ($mailinfo['add_type'] == 1) {
            $org_id = $mailinfo['add_org_id'];
             $sjid = Db::table('cn_organization')
                    ->alias('co')
                    ->where('co.org_id',$org_id)
                    ->field('co.org_name')
                    ->find();
            $sj = $sjid['org_name'];
        }elseif ($mailinfo['add_type'] == 2) {
            $chara_id = $mailinfo['add_chara_id'];
             $sjid = Db::table('cn_character')
                    ->alias('cc')
                    ->where('cc.chara_id',$chara_id)
                    ->field('cc.chara_name')
                    ->find();
            $sj = $sjid['chara_name'];
        }elseif ($mailinfo['add_type'] == 3) {
            $user_id = $mailinfo['add_user_id'];
            $sjid = Db::table('cn_user')
                    ->alias('cu')
                    ->where('cu.user_id',$user_id)
                    ->field('cu.user_name')
                    ->find();
            $sj = $sjid['user_name'];
        }
        // 查询该id所对应的角色，组织，人员
        $oid = Db::table('cn_user_organization_relation') 
                ->alias('cuor')
                ->join('cn_organization co','cuor.org_id = co.org_id')
                ->where('cuor.user_id',$uid)
                ->field('cuor.org_id')
                ->find();
        $cid = Db::table('cn_user_character_relation') 
                ->alias('cucr')
                ->join('cn_character cc','cucr.chara_id = cc.chara_id')
                ->where('cucr.user_id',$uid)
                ->field('cucr.chara_id')
                ->find();
        $onum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_org_id',$oid['org_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $cnum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_chara_id',$cid['chara_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $unum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_user_id',$uid)
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $allnum = array_merge_recursive($onum,$cnum,$unum);
        $num = count($allnum);
        // var_dump($num);die;
        $ores = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_org_id',$oid['org_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $cres = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_chara_id', $cid['chara_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ures = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_user_id',$uid)
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ares = array_merge_recursive($ores,$cres,$ures);
        $num2 = (count($ares));
        if (empty($num)) {
            $num = 0;
             $info = [
                'node' => $list,
                'mailinfo' => $mailinfo,
                'num' => $num,
                'num2' => $num2,
                'sj' => $sj
            ];
        }else{
            $info = [
                'node' => $list,
                'mailinfo' => $mailinfo,
                'num' => $num,
                'num2' => $num2,
                'sj' => $sj
            ];
        }
           
        return $info;
    }

    // 站内消息搜索
    public function getSearch()
    {
        $uid = $_SESSION['think']['username'][0]['user_id'];
        $time =  date('Y-m-d H:i:s',time());
         $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];
        if ($prevdata !== '收件箱消息搜索') {
            //记录用户操作
            $data = [
                'user_id' => $uid,  
                'log_action' => 'Inbox/getSearch',   
                'log_desc' => '收件箱消息搜索',   //默认空
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $ires = db('_user_log')
                ->insert($data);
        }
       
        $list = Db::table('cn_user')
                ->alias('cu')
                ->join('cn_user_character_relation cucr','cu.user_id = cucr.user_id')
                ->join('cn_character cc','cc.chara_id = cucr.chara_id')
                ->join('cn_menu_character_relation cmcr','cmcr.chara_id = cc.chara_id')
                ->join('cn_menu cm','cmcr.menu_id = cm.menu_id')
                ->where('cn_user.user_id',$uid)
                ->field('menu_name')
                ->select();
        $keyword = $_GET['search'];
        $res = Db::table('cn_mail')
                        ->where('is_delete','eq',0)
                        ->where('mail_title|mail_content','like','%'.$keyword.'%')
                        ->field('mail_title,mail_content,create_time,mail_id,is_delete')
                        ->select();
        $title = '收件箱';
        // 查询该id所对应的角色，组织，人员
        $oid = Db::table('cn_user_organization_relation') 
                ->alias('cuor')
                ->join('cn_organization co','cuor.org_id = co.org_id')
                ->where('cuor.user_id',$uid)
                ->field('cuor.org_id')
                ->find();
        $cid = Db::table('cn_user_character_relation') 
                ->alias('cucr')
                ->join('cn_character cc','cucr.chara_id = cc.chara_id')
                ->where('cucr.user_id',$uid)
                ->field('cucr.chara_id')
                ->find();
       
        $onum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_org_id',$oid['org_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $cnum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_chara_id',$cid['chara_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $unum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_user_id',$uid)
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $allnum = array_merge_recursive($onum,$cnum,$unum);
        $num = count($allnum);
        // var_dump($num);die;
        $ores = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_org_id',$oid['org_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $cres = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_chara_id', $cid['chara_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ures = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_user_id',$uid)
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ares = array_merge_recursive($ores,$cres,$ures);
       
       if (empty($ares)) {
            $ares = null;
        }
        $num2 = (count($ares));
        
       
        if (empty($num)) {
            $num = 0;
            if (empty($num2)) {
                $num = 0;
                $info = [
                'list' => $list,
                'res' => $res,
                'num' => $num,
                'num2' => $num2,
                'keyword' => $keyword,
                'title' => $title
            ];
            }
             $info = [
                'list' => $list,
                'res' => $res,
                'num' => $num,
                'num2' => $num2,
                'keyword' => $keyword,
                'title' => $title
            ];
        }else{
            $info = [
                'list' => $list,
                'res' => $res,
                'num' => $num,
                'num2' => $num2,
                'keyword' => $keyword,
                'title' => $title
            ];
        }
        return $info;                
    }

    // 已发送消息搜索
    public function getyfsSearch()
    {
        $uid = $_SESSION['think']['username'][0]['user_id'];
        $time =  date('Y-m-d H:i:s',time());
         $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];
        if ($prevdata !== '站内消息搜索') {
            //记录用户操作
            $data = [
                'user_id' => $uid,  
                'log_action' => 'Inbox/getyfsSearch',   
                'log_desc' => '已发送消息搜索',   //默认空
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $ires = db('_user_log')
                ->insert($data);
        }
       
        $list = Db::table('cn_user')
                ->alias('cu')
                ->join('cn_user_character_relation cucr','cu.user_id = cucr.user_id')
                ->join('cn_character cc','cc.chara_id = cucr.chara_id')
                ->join('cn_menu_character_relation cmcr','cmcr.chara_id = cc.chara_id')
                ->join('cn_menu cm','cmcr.menu_id = cm.menu_id')
                ->where('cn_user.user_id',$uid)
                ->field('menu_name')
                ->select();
        $keyword = $_GET['search'];
        $res = Db::table('cn_mail')
                        ->where('is_delete','eq',0)
                        ->where('create_user_id',$uid)
                        ->where('mail_title|mail_content','like','%'.$keyword.'%')
                        ->field('mail_title,mail_content,create_time,mail_id,is_delete')
                        ->select();
        $title = '已发送';
        // 查询该id所对应的角色，组织，人员
        $oid = Db::table('cn_user_organization_relation') 
                ->alias('cuor')
                ->join('cn_organization co','cuor.org_id = co.org_id')
                ->where('cuor.user_id',$uid)
                ->field('cuor.org_id')
                ->find();
        $cid = Db::table('cn_user_character_relation') 
                ->alias('cucr')
                ->join('cn_character cc','cucr.chara_id = cc.chara_id')
                ->where('cucr.user_id',$uid)
                ->field('cucr.chara_id')
                ->find();
       
        $onum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_org_id',$oid['org_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $cnum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_chara_id',$cid['chara_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $unum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_user_id',$uid)
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $allnum = array_merge_recursive($onum,$cnum,$unum);
        $num = count($allnum);
        // var_dump($num);die;
        $ores = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_org_id',$oid['org_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $cres = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_chara_id', $cid['chara_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ures = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_user_id',$uid)
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ares = array_merge_recursive($ores,$cres,$ures);
       
        if (empty($ares)) {
            $ares = null;
        }
        $num2 = (count($ares));
        
        
        
        if (empty($num)) {
            $num = 0;
            if (empty($num2)) {
                $num = 0;
                $info = [
                'list' => $list,
                'res' => $res,
                'num' => $num,
                'num2' => $num2,
                'keyword' => $keyword,
                'title' => $title
            ];
            }
             $info = [
                'list' => $list,
                'res' => $res,
                'num' => $num,
                'num2' => $num2,
                'keyword' => $keyword,
                'title' => $title
            ];
        }else{
            $info = [
                'list' => $list,
                'res' => $res,
                'num' => $num,
                'num2' => $num2,
                'keyword' => $keyword,
                'title' => $title
            ];
        }
        return $info;                
    }


    // 垃圾箱消息搜索
    public function getljxSearch()
    {
        $uid = $_SESSION['think']['username'][0]['user_id'];
        $time =  date('Y-m-d H:i:s',time());
         $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];
        if ($prevdata !== '已删除消息搜索') {
            //记录用户操作
            $data = [
                'user_id' => $uid,  
                'log_action' => 'Inbox/getljxSearch',   
                'log_desc' => '已删除消息搜索', 
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $ires = db('_user_log')
                ->insert($data);
        }
       
        $list = Db::table('cn_user')
                ->alias('cu')
                ->join('cn_user_character_relation cucr','cu.user_id = cucr.user_id')
                ->join('cn_character cc','cc.chara_id = cucr.chara_id')
                ->join('cn_menu_character_relation cmcr','cmcr.chara_id = cc.chara_id')
                ->join('cn_menu cm','cmcr.menu_id = cm.menu_id')
                ->where('cn_user.user_id',$uid)
                ->field('menu_name')
                ->select();
        $keyword = $_GET['search'];
        $res = Db::table('cn_mail')
                        ->where('is_delete','eq','1')
                        ->where('mail_title|mail_content','like','%'.$keyword.'%')
                        ->field('mail_title,mail_content,create_time,mail_id,is_delete')
                        ->select();
        $title = '已删除';
        // 查询该id所对应的角色，组织，人员
        $oid = Db::table('cn_user_organization_relation') 
                ->alias('cuor')
                ->join('cn_organization co','cuor.org_id = co.org_id')
                ->where('cuor.user_id',$uid)
                ->field('cuor.org_id')
                ->find();
                // var_dump($oid);die;
        $cid = Db::table('cn_user_character_relation') 
                ->alias('cucr')
                ->join('cn_character cc','cucr.chara_id = cc.chara_id')
                ->where('cucr.user_id',$uid)
                ->field('cucr.chara_id')
                ->find();
       
        $onum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_org_id',$oid['org_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $cnum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_chara_id',$cid['chara_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $unum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_user_id',$uid)
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $allnum = array_merge_recursive($onum,$cnum,$unum);
        $num = count($allnum);
        // var_dump($num);die;
        $ores = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_org_id',$oid['org_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $cres = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_chara_id', $cid['chara_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ures = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_user_id',$uid)
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ares = array_merge_recursive($ores,$cres,$ures);
        $num2 = (count($ares));
       
        if (empty($ares)) {
            $ares = null;
        }
        
        
        
        if (empty($num)) {
            $num = 0;
            if (empty($num2)) {
                $num = 0;
                $info = [
                'list' => $list,
                'res' => $res,
                'num' => $num,
                'num2' => $num2,
                'keyword' => $keyword,
                'title' => $title
            ];
            }
             $info = [
                'list' => $list,
                'res' => $res,
                'num' => $num,
                'num2' => $num2['0']['count'],
                'keyword' => $keyword,
                'title' => $title
            ];
        }else{
            $info = [
                'list' => $list,
                'res' => $res,
                'num' => $num['0']['count'],
                'num2' => $num2,
                'keyword' => $keyword,
                'title' => $title
            ];
        }
        return $info;                
    }

    // 公告消息搜索
    public function getnoticeSearch()
    {
        $uid = $_SESSION['think']['username'][0]['user_id'];
        $time =  date('Y-m-d H:i:s',time());
         $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];
        if ($prevdata !== '公告消息搜索') {
            //记录用户操作
            $data = [
                'user_id' => $uid,  
                'log_action' => 'Inbox/getnoticeSearch',   
                'log_desc' => '公告消息搜索', 
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $ires = db('_user_log')
                ->insert($data);
        }
       
        $list = Db::table('cn_user')
                ->alias('cu')
                ->join('cn_user_character_relation cucr','cu.user_id = cucr.user_id')
                ->join('cn_character cc','cc.chara_id = cucr.chara_id')
                ->join('cn_menu_character_relation cmcr','cmcr.chara_id = cc.chara_id')
                ->join('cn_menu cm','cmcr.menu_id = cm.menu_id')
                ->where('cn_user.user_id',$uid)
                ->field('menu_name')
                ->select();
        $keyword = $_GET['search'];
        $res = Db::table('cn_mail')
                        ->where('is_delete','eq','0')
                        ->where('mail_send_type','eq','1')
                        ->where('mail_title|mail_content','like','%'.$keyword.'%')
                        ->field('mail_title,mail_content,create_time,mail_id,is_delete')
                        ->select();
        $title = '公告';
        // 查询该id所对应的角色，组织，人员
        $oid = Db::table('cn_user_organization_relation') 
                ->alias('cuor')
                ->join('cn_organization co','cuor.org_id = co.org_id')
                ->where('cuor.user_id',$uid)
                ->field('cuor.org_id')
                ->find();
        $cid = Db::table('cn_user_character_relation') 
                ->alias('cucr')
                ->join('cn_character cc','cucr.chara_id = cc.chara_id')
                ->where('cucr.user_id',$uid)
                ->field('cucr.chara_id')
                ->find();
       
        $onum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_org_id',$oid['org_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $cnum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_chara_id',$cid['chara_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $unum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_user_id',$uid)
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $allnum = array_merge_recursive($onum,$cnum,$unum);
        $num = count($allnum);
        // var_dump($num);die;
        $ores = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_org_id',$oid['org_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $cres = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_chara_id', $cid['chara_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ures = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_user_id',$uid)
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ares = array_merge_recursive($ores,$cres,$ures);
       
        if (empty($ares)) {
            $ares = null;
        }
        $num2 = (count($ares));
        
   
        if (empty($num)) {
            $num = 0;
            if (empty($num2)) {
                $num = 0;
                $info = [
                'list' => $list,
                'res' => $res,
                'num' => $num,
                'num2' => $num2,
                'keyword' => $keyword,
                'title' => $title
            ];
            }
             $info = [
                'list' => $list,
                'res' => $res,
                'num' => $num,
                'num2' => $num2['0']['count'],
                'keyword' => $keyword,
                'title' => $title
            ];
        }else{
            $info = [
                'list' => $list,
                'res' => $res,
                'num' => $num['0']['count'],
                'num2' => $num2,
                'keyword' => $keyword,
                'title' => $title
            ];
        }
        return $info;                
    }

    public function getNotice()
    {
        $uid = $_SESSION['think']['username'][0]['user_id'];
        $time =  date('Y-m-d H:i:s',time());
        $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];
        if ($prevdata !== '写公告') {
            //记录用户操作
            $data = [
                'user_id' => $uid,  
                'log_action' => 'Inbox/getNotice',   
                'log_desc' => '写公告',  
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $ires = db('_user_log')
                ->insert($data);
        }
       
        $list = Db::table('cn_user')
                ->alias('cu')
                ->join('cn_user_character_relation cucr','cu.user_id = cucr.user_id')
                ->join('cn_character cc','cc.chara_id = cucr.chara_id')
                ->join('cn_menu_character_relation cmcr','cmcr.chara_id = cc.chara_id')
                ->join('cn_menu cm','cmcr.menu_id = cm.menu_id')
                ->where('cn_user.user_id',$uid)
                ->field('menu_name')
                ->select();
        //获取所有人员、角色、组织（去除自己）
        // $alluser = Db::table('cn_user')
        //         ->where('user_id','neq',$uid)
        //         ->field('user_name')
        //         ->select();
        // for ($i=0; $i < count($alluser) ; $i++) { 
        //     $au[$i]['name'] = $alluser[$i]['user_name'];
        // }
         
        // $allchar = Db::table('cn_character')
        //         ->field('chara_name')
        //         ->select();
        // for ($i=0; $i < count($allchar) ; $i++) { 
        //     $ac[$i]['name'] = $allchar[$i]['chara_name'];
        // }
       
        
        $bmparent = Db::table('cn_organization')
                    ->where('is_delete','0')
                    ->orderRaw('concat(path,org_id,",")')
                    ->select();
                    // var_dump($org);die;
        $all = array();
        foreach ($bmparent as $k=>$v) {
            // 统计多少级分类
            $level = substr_count($v['path'], ','); // 统计','出现的次数 
            $v['level'] = $level;
            $v['html'] = str_repeat('---', $level-1); // str_repeat(重复字符串,重复次数)
            $all[] = $v;
        }
        //加入所有部门
        $top[] = [
            'org_id' => 0,
            'org_name' => '所有部门',
        ];
        $all = array_merge($all,$top);
        // dump($all);die;
        // dump($top);die;
        // for ($i=0; $i < count($allorg) ; $i++) { 
        //     $ao[$i]['name'] = $allorg[$i]['org_name'];
        // }
        // // var_dump($ao);die;
        //         // var_dump($allorg);die;
        // // foreach($allorg as $j=>$r){
        // //     $m[$j] = $r['org_name'];
        // //     $m[$j] = $r['org_name'];
        // // }
        // // 信息合并到一个二维数组
        // $all = $ao;
        // var_dump($all);die;
        // 查询该id所对应的角色，组织，人员
        $oid = Db::table('cn_user_organization_relation') 
                ->alias('cuor')
                ->join('cn_organization co','cuor.org_id = co.org_id')
                ->where('cuor.user_id',$uid)
                ->field('cuor.org_id')
                ->find();
                // var_dump($oid);die;
        $cid = Db::table('cn_user_character_relation') 
                ->alias('cucr')
                ->join('cn_character cc','cucr.chara_id = cc.chara_id')
                ->where('cucr.user_id',$uid)
                ->field('cucr.chara_id')
                ->find();
       
        $onum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_org_id',$oid['org_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $cnum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_chara_id',$cid['chara_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $unum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_user_id',$uid)
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $allnum = array_merge_recursive($onum,$cnum,$unum);
        $num = count($allnum);
        // var_dump($num);die;
        $ores = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_org_id',$oid['org_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $cres = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_chara_id', $cid['chara_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ures = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_user_id',$uid)
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ares = array_merge_recursive($ores,$cres,$ures);
        $num2 = (count($ares));
       
    
        if (empty($num)) {
            $num = 0;
            if (empty($num)) {
                $num2 = 0;
                $info = [
                'all' => $all,
                'node' => $list,
                'num' => $num,
                'num2' => $num
            ];
            }
             $info = [
                'all' => $all,
                'node' => $list,
                'num' => $num,
                'num2' => $num
            ];
        }else{
            $info = [
                'all' => $all,
                'node' => $list,
                'num' => $num,
                'num2' => $num2
            ];
        }
        
        return $info;
    }

    // 公告
    public function sendNotice()
    {
        // dump($_POST);die;
        $uid = $_SESSION['think']['username'][0]['user_id'];
        $time =  date('Y-m-d H:i:s',time());
        $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];
        if ($prevdata !== '发送公告') {
            //记录用户操作
            $data = [
                'user_id' => $uid,  
                'log_action' => 'Inbox/sendNotice',   
                'log_desc' => '发送公告', 
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $ires = db('_user_log')
                ->insert($data);
        }
       
        $list = Db::table('cn_user')
                ->alias('cu')
                ->join('cn_user_character_relation cucr','cu.user_id = cucr.user_id')
                ->join('cn_character cc','cc.chara_id = cucr.chara_id')
                ->join('cn_menu_character_relation cmcr','cmcr.chara_id = cc.chara_id')
                ->join('cn_menu cm','cmcr.menu_id = cm.menu_id')
                ->where('cn_user.user_id',$uid)
                ->field('menu_name')
                ->select();
        
        //写入cn_mail
        if (empty($_POST['editorValue'])) {
            $_POST['editorValue'] = '';
        }
        $data = [
            'mail_title' => $_POST['subject'],
            'mail_content' => $_POST['editorValue'],
            'mail_type' => $_POST['type'],
            'mail_file' => '0',   //先写死
            'mail_send_type' => '1',
            'mail_type' => $_POST['type'],
            'mail_secrecy' => '0', //默认不保密
            'mail_send_channel' => '2',  
            'create_user_id' => $uid,
            'create_time' => $time,
            'update_user_id' => $uid,
            'update_time' => $time,
            'is_read' => '0',
            'is_delete' => '0'
        ];
        $res = db('_mail')
            ->insertGetId($data);
        $to = $_POST['to'];
        // dump($to);die;
        // $t = $to[0];
            for ($i=0; $i < count($to) ; $i++) { 
                # code...
            if ($to[$i] == '所有部门') {
                $org_id[0] = '-1';

            }else{

               // 根据收件人名获取id
                $req[$i] = Db::table('cn_organization')
                        ->alias('co')
                        ->where('co.org_name',$to[$i])
                        ->field('org_id')
                        ->find();
                // var_dump($req);die;
                $org_id[$i] = $req[$i]['org_id'];
                }
            }
            
            // foreach( $org_id as $k=>$v){   
            //     if( !$v )   
            //         unset( $org_id[$k] );   
            // }   
            // if ($org_id) {
                // var_dump(123);die;
                for ($j=0; $j < count($org_id); $j++) { 
                
                $info[$j] = [
                    'add_type' => '1',
                    'add_org_id' => $org_id[$j],
                    'add_chara_id' => '0',
                    'add_user_id' => '0',
                    'mail_id' => $res,
                    'create_user_id' => $uid,
                    'create_time' => $time,
                    'update_user_id' => $uid,
                    'update_time' => $time,
                    'is_delete' => '0',
                ];
                $a = db('_mail_addressee')
                    ->insert($info[$j]);
                }
                        $b = [
                            'a' =>$a,
                            'node' => $list
                        ];
                    return $b;
            // }else{
            //     for ($n=0; $n < count($to); $n++) { 
            //         # code...
                
            //     $rew[$n] = Db::table('cn_user')
            //     ->alias('cu')
            //     ->where('cu.user_name',$to)
            //     ->field('user_id')
            //     ->find();
            //     $user_id[$n] = $rew[$n]['user_id'];
            //     }
            //     foreach( $user_id as $k=>$v){   
            //     if( !$v )   
            //         unset( $user_id[$k] );   
            //     }   
            //     if ($user_id) {
            //         for ($m=0; $m < count($user_id) ; $m++) { 
            //             # code...
                    
            //         $info[$m] = [
            //             'add_type' => '3',
            //             'add_org_id' => '0',
            //             'add_chara_id' => '0',
            //             'add_user_id' => $user_id[$m],
            //             'mail_id' => $res,
            //             'create_user_id' => $uid,
            //             'create_time' => $time,
            //             'update_user_id' => $uid,
            //             'update_time' => $time,
            //             'is_delete' => '0',
            //         ];
            //         $a = db('_mail_addressee')
            //             ->insert($info[$m]);
            //         }
            //             $b = [
            //                 'a' =>$a,
            //                 'node' => $list
            //                 ];
            //             return $b;
            //     }else{
            //         for ($v=0; $v < count($to); $v++) { 
            //             # code...
                    
            //            $ree[$v] = Db::table('cn_character')
            //             ->alias('cc')
            //             ->where('cc.chara_name',$to)
            //             ->field('chara_id')
            //             ->find();
            //         $chara_id[$v] = $ree[$v]['chara_id'];
                    
            //         $info[$v] = [
            //             'add_type' => '2',
            //             'add_org_id' => '0' ,
            //             'add_chara_id' => $chara_id[$v],
            //             'add_user_id' => '0',
            //             'mail_id' => $res,
            //             'create_user_id' => $uid,
            //             'create_time' => $time,
            //             'update_user_id' => $uid,
            //             'update_time' => $time,
            //             'is_delete' => '0',
            //         ];
            //         $a = db('_mail_addressee')
            //             ->insert($info[$v]);
            //         }
            //             return $a;
            //             $b = [
            //                 'a' =>$a,
            //                 'node' => $list
            //                 ];
            //             return $b;
            //     }
                
            // }
    }

    // 公告列表  。。。缩略
    public function getAbnotice()
    {
        $uid = $_SESSION['think']['username'][0]['user_id'];
        $time =  date('Y-m-d H:i:s',time());
        $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];
        if ($prevdata !== '发送公告') {
            //记录用户操作
            $data = [
                'user_id' => $uid,  
                'log_action' => 'Inbox/getAbnotice',   
                'log_desc' => '浏览公告消息',   
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $ires = db('_user_log')
                ->insert($data);
        }
   
        $list = Db::table('cn_user')
                ->alias('cu')
                ->join('cn_user_character_relation cucr','cu.user_id = cucr.user_id')
                ->join('cn_character cc','cc.chara_id = cucr.chara_id')
                ->join('cn_menu_character_relation cmcr','cmcr.chara_id = cc.chara_id')
                ->join('cn_menu cm','cmcr.menu_id = cm.menu_id')
                ->where('cn_user.user_id',$uid)
                ->field('menu_name')
                ->select();
        // 查询该id所对应的角色，组织，人员
        $oid = Db::table('cn_user_organization_relation') 
                ->alias('cuor')
                ->join('cn_organization co','cuor.org_id = co.org_id')
                ->where('cuor.user_id',$uid)
                ->field('cuor.org_id')
                ->find();
        $cid = Db::table('cn_user_character_relation') 
                ->alias('cucr')
                ->join('cn_character cc','cucr.chara_id = cc.chara_id')
                ->where('cucr.user_id',$uid)
                ->field('cucr.chara_id')
                ->find();
        // $omail = Db::table('cn_mail_addressee')
        //         ->alias('cma')
        //         ->join('cn_mail cm','cm.mail_id = cma.mail_id')
        //         ->where('cma.add_org_id',$oid['org_id'])
        //         ->where('cm.mail_send_channel','eq','2')
        //         ->where('mail_send_type','eq','1')
        //         ->field('cm.mail_title,cm.mail_show_content,cm.create_time,cm.mail_id,cm.is_read')
        //         ->order('create_time desc')
        //         ->select();
        
        $sql = "SELECT
                    cm.mail_title,
                    cm.mail_show_content,
                    cm.create_time,
                    cm.mail_id,
                    cm.is_read 
                FROM
                    cn_mail_addressee AS cma
                    LEFT JOIN cn_mail AS cm ON cm.mail_id = cma.mail_id 
                WHERE
                    cma.add_org_id = ".$oid['org_id']." 
                    OR cma.add_org_id = - 1 
                    AND cm.mail_send_channel = 2 
                    AND mail_send_type = 1 
                ORDER BY
                    create_time DESC";
        // dump($sql);die;
        $omail = Db::query($sql);
        // var_dump($omail);die;
        // $cmail = Db::table('cn_mail_addressee')
        //         ->alias('cma')
        //         ->join('cn_mail cm','cm.mail_id = cma.mail_id')
        //         ->where('cma.add_chara_id',$cid['chara_id'])
        //         ->where('cm.mail_send_channel','eq','2')
        //         ->where('mail_send_type','eq','1')
        //         ->field('cm.mail_title,cm.mail_show_content,cm.create_time,cm.mail_id,cm.is_read')
        //         ->order('create_time desc')
        //         ->select();
                
        // $umail = Db::table('cn_mail_addressee')
        //         ->alias('cma')
        //         ->join('cn_mail cm','cm.mail_id = cma.mail_id')
        //         ->where('cma.add_user_id',$uid)
        //         ->where('cm.mail_send_channel','eq','2')
        //         ->where('mail_send_type','eq','1')
        //         ->field('cm.mail_title,cm.mail_show_content,cm.create_time,cm.mail_id,cm.is_read')
        //         ->order('create_time desc')
        //         ->select();
                
        
        $abnotice = $omail;
        // $page = $abnotice->render();
        if (empty($abnotice)) {
            $amount = 0;
        }
        $amount = $abnotice;

        $onum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_org_id',$oid['org_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $cnum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_chara_id',$cid['chara_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $unum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_user_id',$uid)
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $allnum = array_merge_recursive($onum,$cnum,$unum);
        $num = count($allnum);
        // var_dump($num);die;
        $ores = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_org_id',$oid['org_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $cres = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_chara_id', $cid['chara_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ures = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_user_id',$uid)
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ares = array_merge_recursive($ores,$cres,$ures);
        $num2 = (count($ares));
        // var_dump($num2);die;
        
        if (empty($num)) {
            $num = 0;
            if (empty($num2)) {
                 $num2 = 0;
                 $info = [
                'notice' => $amount,
                'list' => $list,
                'num' => $num,
                'num2' => $num2
                // 'page' => $page
            ];
            }
             $info = [
                'notice' => $amount,
                'list' => $list,
                'num' => $num,
                'num2' => $num2
                // 'page' => $page
            ];
        }else{
            $info = [
                'notice' => $amount,
                'list' => $list,
                'num' => $num,
                'num2' => $num2
                // 'page' => $page
            ];
        }
        return $info;
    }


    public function restoreMail($id)
    {
        $uid = $_SESSION['think']['username'][0]['user_id'];
        $time =  date('Y-m-d H:i:s',time());
        $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];
        if ($prevdata !== '还原消息') {
            //记录用户操作
            $data = [
                'user_id' => $uid,  
                'log_action' => 'Inbox/restoreMail',   
                'log_desc' => '还原消息',   
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $ires = db('_user_log')
                ->insert($data);
        }
        for ($i=0; $i < count($id); $i++) { 
            $code[] = $id[$i];
        
        $stom = Db::table('cn_mail')
                ->alias('cm')
                ->where('cm.mail_id',$code[$i])
                ->update(['is_delete'=>0,'update_time'=>$time,'update_user_id'=>$uid]);
        $stom = Db::table('cn_mail_addressee')
                ->alias('cma')
                ->where('cma.mail_id',$code[$i])
                ->update(['is_delete'=>0,'update_time'=>$time,'update_user_id'=>$uid]);
        }
         return $stom;
    }

    // 彻底删除
    public function finaldelMail($id)
    {
        $uid = $_SESSION['think']['username'][0]['user_id'];
        $time =  date('Y-m-d H:i:s',time());
        $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];
        if ($prevdata !== '彻底删除消息') {
            //记录用户操作
            $data = [
                'user_id' => $uid,  
                'log_action' => 'Inbox/finaldelMail',   
                'log_desc' => '彻底删除消息',   
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $ires = db('_user_log')
                ->insert($data);
        }
        for ($i=0; $i < count($id); $i++) { 
            $code[] = $id[$i];
        // var_dump($code);die;
        $fdelm = Db::table('cn_mail')
                ->alias('cm')
                ->where('cm.mail_id',$code[$i])
                ->update(['is_delete'=>2,'update_time'=>$time,'update_user_id'=>$uid]);
        $ffdelm = Db::table('cn_mail_addressee')
                ->alias('cma')
                ->where('cma.mail_id',$code[$i])
                ->update(['is_delete'=>2,'update_time'=>$time,'update_user_id'=>$uid]);
        }
      
         return $fdelm;
    }

    // 回复消息
    public function replyMail($id)
    {
        $uid = $_SESSION['think']['username'][0]['user_id'];
        $time =  date('Y-m-d H:i:s',time());
        $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];
        if ($prevdata !== '回复消息') {
            //记录用户操作
            $data = [
                'user_id' => $uid,  
                'log_action' => 'Inbox/replyMail',   
                'log_desc' => '回复消息',   
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $ires = db('_user_log')
                ->insert($data);
        }
        $list = Db::table('cn_user')
                ->alias('cu')
                ->join('cn_user_character_relation cucr','cu.user_id = cucr.user_id')
                ->join('cn_character cc','cc.chara_id = cucr.chara_id')
                ->join('cn_menu_character_relation cmcr','cmcr.chara_id = cc.chara_id')
                ->join('cn_menu cm','cmcr.menu_id = cm.menu_id')
                ->where('cn_user.user_id',$uid)
                ->field('menu_name')
                ->select();
        
        // 根据消息id查询消息内容
        $mailinfo = Db::table('cn_mail_addressee')
                    ->alias('cma')
                    ->join('cn_mail cm','cm.mail_id = cma.mail_id')
                    ->where('cma.mail_id',$id)
                    ->where('cma.is_delete','eq','0')
                    ->where('cm.is_delete','eq','0')
                    ->field('cm.mail_content,cm.mail_title,mail_type,cma.create_time,cma.add_type,cma.add_org_id,cma.add_user_id,cma.add_chara_id')
                    ->find();
        if ($mailinfo['add_org_id']) {
            //查询org_id对应org
            $fj = Db::table('cn_organization')
                    ->where('org_id',$mailinfo['add_org_id'])
                    ->field('org_name')
                    ->find();
            $fj = $fj['org_name'];
        }else if($mailinfo['add_chara_id']){
            //查询chara_id对应chara
            $fj = Db::table('cn_character')
                    ->where('chara_id',$mailinfo['add_chara_id'])
                    ->field('chara_name')
                    ->find();
            $fj = $fj['chara_name'];
        }else{
            //查询user_id对应user
            $fj = Db::table('cn_user')
                    ->where('user_id',$mailinfo['add_user_id'])
                    ->field('user_name')
                    ->find();
            $fj = $fj['user_name'];
        }

        // 查询该id所对应的角色，组织，人员
        $oid = Db::table('cn_user_organization_relation') 
                ->alias('cuor')
                ->join('cn_organization co','cuor.org_id = co.org_id')
                ->where('cuor.user_id',$uid)
                ->field('cuor.org_id')
                ->find();
                // var_dump($oid);die;
        $cid = Db::table('cn_user_character_relation') 
                ->alias('cucr')
                ->join('cn_character cc','cucr.chara_id = cc.chara_id')
                ->where('cucr.user_id',$uid)
                ->field('cucr.chara_id')
                ->find();
       
        $onum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_org_id',$oid['org_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $cnum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_chara_id',$cid['chara_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $unum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_user_id',$uid)
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $allnum = array_merge_recursive($onum,$cnum,$unum);
        $num = count($allnum);
        // var_dump($num);die;
        $ores = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_org_id',$oid['org_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $cres = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_chara_id', $cid['chara_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ures = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_user_id',$uid)
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ares = array_merge_recursive($ores,$cres,$ures);
        $num2 = (count($ares));
        if (empty($num)) {
            $num = 0;
            if (empty($num2)) {
                $num2 = 0;
                $info = [
                'node' => $list,
                'mailinfo' => $mailinfo,
                'fj' => $fj,
                'num' => $num,
                'num2' => $num2
            ];
            }
             $info = [
                'node' => $list,
                'mailinfo' => $mailinfo,
                'fj' => $fj,
                'num' => $num,
                'num2' => $num2
            ];
        }else{
            $info = [
                'node' => $list,
                'mailinfo' => $mailinfo,
                'fj' => $fj,
                'num' => $num,
                'num2' => $num2
            ];
        }
        return $info;
    }

    // 转发消息
    public function forwardMail($id)
    {
        $uid = $_SESSION['think']['username'][0]['user_id'];
        $time =  date('Y-m-d H:i:s',time());
        $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];
        if ($prevdata !== '转发消息') {
            //记录用户操作
            $data = [
                'user_id' => $uid,  
                'log_action' => 'Inbox/forwardMail',   
                'log_desc' => '转发消息', 
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $ires = db('_user_log')
                ->insert($data);
        }
        $list = Db::table('cn_user')
                ->alias('cu')
                ->join('cn_user_character_relation cucr','cu.user_id = cucr.user_id')
                ->join('cn_character cc','cc.chara_id = cucr.chara_id')
                ->join('cn_menu_character_relation cmcr','cmcr.chara_id = cc.chara_id')
                ->join('cn_menu cm','cmcr.menu_id = cm.menu_id')
                ->where('cn_user.user_id',$uid)
                ->field('menu_name')
                ->select();
        
        // 根据消息id查询消息内容
        $mailinfo = Db::table('cn_mail_addressee')
                    ->alias('cma')
                    ->join('cn_mail cm','cm.mail_id = cma.mail_id')
                    ->where('cma.mail_id',$id)
                    ->where('cma.is_delete','eq','0')
                    ->where('cm.is_delete','eq','0')
                    ->field('cm.mail_content,cm.mail_title,mail_type,cma.create_time,cma.add_type,cma.add_org_id,cma.add_user_id,cma.add_chara_id')
                    ->find();
        if ($mailinfo['add_org_id']) {
            //查询org_id对应org
            $fj = Db::table('cn_organization')
                    ->where('org_id',$mailinfo['add_org_id'])
                    ->field('org_name')
                    ->find();
            $fj = $fj['org_name'];
        }else if($mailinfo['add_chara_id']){
            //查询chara_id对应chara
            $fj = Db::table('cn_character')
                    ->where('chara_id',$mailinfo['add_chara_id'])
                    ->field('chara_name')
                    ->find();
            $fj = $fj['chara_name'];
        }else{
            //查询user_id对应user
            $fj = Db::table('cn_user')
                    ->where('user_id',$mailinfo['add_user_id'])
                    ->field('user_name')
                    ->find();
            $fj = $fj['user_name'];
        }

        // 查询该id所对应的角色，组织，人员
        $oid = Db::table('cn_user_organization_relation') 
                ->alias('cuor')
                ->join('cn_organization co','cuor.org_id = co.org_id')
                ->where('cuor.user_id',$uid)
                ->field('cuor.org_id')
                ->find();
        $cid = Db::table('cn_user_character_relation') 
                ->alias('cucr')
                ->join('cn_character cc','cucr.chara_id = cc.chara_id')
                ->where('cucr.user_id',$uid)
                ->field('cucr.chara_id')
                ->find();
       
        $onum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_org_id',$oid['org_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $cnum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_chara_id',$cid['chara_id'])
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $unum = Db::table('cn_mail')
                    ->alias('cm')
                    ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                    ->where('cm.is_delete','eq','0')
                    ->where('cm.is_read','eq','0')
                    ->where('cma.add_user_id',$uid)
                    ->where('cm.mail_send_type','eq','2')
                    ->field('count(cm.mail_id) as count')
                    ->group('cm.mail_id')
                    ->select();
        $allnum = array_merge_recursive($onum,$cnum,$unum);
        $num = count($allnum);
        // var_dump($num);die;
        $ores = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_org_id',$oid['org_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $cres = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_chara_id', $cid['chara_id'])
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ures = Db::table('cn_mail')
                ->alias('cm')
                ->join('cn_mail_addressee cma','cma.mail_id = cm.mail_id')
                ->where('cma.add_user_id',$uid)
                ->where('cm.is_delete','eq',0)
                ->where('cm.is_read','eq',0)
                ->where('cm.mail_send_channel','eq',2)
                ->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
                ->group('cm.mail_id')
                ->select();
        $ares = array_merge_recursive($ores,$cres,$ures);
        $num2 = (count($ares));
      
        if (empty($num)) {
            $num = 0;
            if (empty($num2)) {
                $num2 = 0;
                $info = [
                'node' => $list,
                'mailinfo' => $mailinfo,
                'fj' => $fj,
                'num' => $num,
                'num2' => $num2
            ];
            }
             $info = [
                'node' => $list,
                'mailinfo' => $mailinfo,
                'fj' => $fj,
                'num' => $num,
                'num2' => $num2
            ];
        }else{
            $info = [
                'node' => $list,
                'mailinfo' => $mailinfo,
                'fj' => $fj,
                'num' => $num,
                'num2' => $num2
            ];
        }
        return $info;
    }
    
    
}

?>